/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.studentevaluate.service;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.config.shiro.redis.RedisCache;
import com.jeeplus.modules.graph.entity.ByReason;
import com.jeeplus.modules.graph.entity.PinYin;
import com.jeeplus.modules.sys.entity.StudentMoreduMain;
import liquibase.pro.packaged.V;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.studentevaluate.entity.StudentEvaluate;
import com.jeeplus.modules.studentevaluate.mapper.StudentEvaluateMapper;

/**
 * 学生评价Service
 * @author 秦子洋
 * @version 2022-03-02
 */
@Service
@Transactional(readOnly = true)
public class StudentEvaluateService extends CrudService<StudentEvaluateMapper, StudentEvaluate> {

//	@Autowired
//	private RedisTemplate<String,String> redisTemplate;
	@Autowired
	@Qualifier("longRedisTemplate")
	private RedisTemplate<String,Object> redisTemplateObject;

	public StudentEvaluate get(String id) {
		return super.get(id);
	}
	
	public List<StudentEvaluate> findList(StudentEvaluate studentEvaluate) {
		String queryName =this.getClass().getSimpleName()+".findList"+ studentEvaluate.toString();
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<StudentEvaluate> data;
		// 如果缓存中有则直接返回
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,StudentEvaluate.class);
			logger.info("走了byReason缓存");
			return data;
		}else {
			// 缓存中没有则从数据库中查询
			data = super.findList(studentEvaluate);
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
			return data;
		}
	}
	
	public Page<StudentEvaluate> findPage(Page<StudentEvaluate> page, StudentEvaluate studentEvaluate) {
		dataRuleFilter(studentEvaluate);
		studentEvaluate.setPage(page);
		String queryName =this.getClass().getSimpleName()+".findPage"+ studentEvaluate.toString() + page.getPageNo() + page.getPageSize();
		Page<StudentEvaluate> tempPage;
		// 先去查询缓存
		tempPage = (Page<StudentEvaluate>)redisTemplateObject.opsForValue().get(queryName);
		// 如果缓存中有则直接返回
		if(Objects.nonNull(tempPage)){
			page = tempPage;
			System.out.println("我有数据");
		}else {
			//缓存中没有则从数据库中查询
			page.setList(mapper.findList(studentEvaluate));
			redisTemplateObject.opsForValue().set(queryName,page,2, TimeUnit.DAYS);
		}
		return page;
	}
	public List<PinYin> queryName(){
		String queryName = this.getClass().getSimpleName()+".queryName";
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<PinYin> data;
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,PinYin.class);
			logger.info("走了byReason缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.queryName();
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public List<PinYin> queryInstitute(){
		String queryName = this.getClass().getSimpleName()+".queryInstitute";
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<PinYin> data;
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,PinYin.class);
			logger.info("走了byReason缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.queryInstitute();
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public List<PinYin> queryTeacher(){
		String queryName = this.getClass().getSimpleName()+".queryTeacher";
		// 先去查询缓存
		String reasonListJsonStr = (String)redisTemplateObject.opsForValue().get(queryName);
		List<PinYin> data;
		if(StringUtils.isNoneBlank(reasonListJsonStr)){
			data = JSON.parseArray(reasonListJsonStr,PinYin.class);
			logger.info("走了byReason缓存");
		}else {
			// 缓存中没有则从数据库中查询
			data = mapper.queryTeacher();
			// 放入缓存中，120s失效
			redisTemplateObject.opsForValue().set(queryName,JSON.toJSONString(data),2, TimeUnit.DAYS);
//            logger.info("放入byReason缓存");
		}
		return data;
	}
	public List<PinYin> test(){
		return mapper.test();
	}
	@Transactional(readOnly = false)
	public void save(StudentEvaluate studentEvaluate) {
		super.save(studentEvaluate);
	}
	
	@Transactional(readOnly = false)
	public void delete(StudentEvaluate studentEvaluate) {
//	\xac\xed\x00\x05t\x00\xab
		Set keys = redisTemplateObject.keys("StudentEvaluate" + "*");
		if (ObjectUtil.isNotEmpty(keys)) {
			redisTemplateObject.delete(keys);
		}
		// redisTemplateObject.opsForValue().getOperations().delete("*");
		super.delete(studentEvaluate);
	}
	@Transactional(readOnly = false)
	public void reEvaluate(StudentEvaluate studentEvaluate) {
		studentEvaluate.setEvaluate("未评价");
		studentEvaluate.setSignature("");
		studentEvaluate.setRelationship("");
		studentEvaluate.setTelphone("");
		studentEvaluate.setAddress("");
		mapper.reEvaluate(studentEvaluate);
	}
	@Transactional(readOnly = false)
	public void delall() {
		mapper.delall();
	}


}